package net.alan.modules.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.alan.common.utils.DateUtil;
import net.alan.common.utils.LangUtils;
import net.alan.common.utils.StringUtils;
import net.alan.modules.bean.*;
import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.handle.BaseExecption;
import net.alan.modules.service.IReportService;
import net.alan.modules.service.ISettingService;
import net.alan.modules.service.ITaskService;
import net.alan.modules.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Api(tags = "周日报")
@Controller
@RequestMapping(value = "report")
public class ReportController extends BaseController {
    @Autowired
    private IReportService reportService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ISettingService settingService;

    @ApiOperation(value = "添加日报", notes = "")
    @RequestMapping(value = "addDaily", method = RequestMethod.POST)
    @ResponseBody
    public Result addDaily(Daily daily) throws Exception {
        try {
            daily.setUser(WebContextUtil.getCurrentUser().getUser());
            reportService.save(Daily.class, daily);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "添加日报回复", notes = "")
    @RequestMapping(value = "addDailyItem", method = RequestMethod.POST)
    @ResponseBody
    public Result addDailyItem(DailyItem dailyItem, Integer dailyId) throws Exception {
        try {
            String content = dailyItem.getContent();
            if (content.startsWith("@")) {
                content = content.substring(content.indexOf(" ") + 1, content.length());
                dailyItem.setContent(content);
            }
            dailyItem.setUser(WebContextUtil.getCurrentUser().getUser());
            reportService.save(DailyItem.class, dailyItem);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "添加周报", notes = "")
    @RequestMapping(value = "addWeekly", method = RequestMethod.POST)
    @ResponseBody
    public Result addWeekly(Weekly weekly) throws Exception {
        try {
            weekly.setUser(WebContextUtil.getCurrentUser().getUser());
            reportService.save(Weekly.class, weekly);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "添加周报回复", notes = "")
    @RequestMapping(value = "addWeeklyItem", method = RequestMethod.POST)
    @ResponseBody
    public Result addWeeklyItem(WeeklyItem weeklyItem, Integer weeklyId) throws Exception {
        try {
            String content = weeklyItem.getContent();
            if (content.startsWith("@")) {
                content = content.substring(content.indexOf(" ") + 1, content.length());
                weeklyItem.setContent(content);
            }
            weeklyItem.setUser(WebContextUtil.getCurrentUser().getUser());
            reportService.save(WeeklyItem.class, weeklyItem);
        } catch (BaseExecption baseExecption) {
            return ResultUtil.error("保存异常");
        }
        return ResultUtil.success();
    }

    @ApiOperation(value = "列表周报")
    @RequestMapping(value = "listWeekly", method = RequestMethod.GET)
    @ResponseBody
    public List<Weekly> listWeekly(Integer projectId, String week) throws Exception {
        return this.reportService.listWeeklyByProjectId(projectId, DateUtil.toWeek(week, "yyyy-MM-dd"));
    }

    @ApiOperation(value = "列表日报")
    @RequestMapping(value = "listDaily", method = RequestMethod.GET)
    @ResponseBody
    public List<Daily> listDaily(Integer projectId, String day) throws Exception {
        return this.reportService.listDailyByProjectId(projectId, DateUtil.toDate(day, "yyyy-MM-dd"));
    }

    @ApiOperation(value = "列表周报回复")
    @RequestMapping(value = "listWeeklyItem", method = RequestMethod.GET)
    @ResponseBody
    public List<WeeklyItem> listWeeklyItem(Integer weeklyId) throws Exception {
        return this.reportService.listWeelyItemByDailyId(weeklyId);
    }

    @ApiOperation(value = "列表日报回复")
    @RequestMapping(value = "listDailyItem", method = RequestMethod.GET)
    @ResponseBody
    public List<DailyItem> listDailyItem(Integer dailyId) throws Exception {
        return this.reportService.listDailyItemByDailyId(dailyId);
    }

    @ApiOperation(value = "周日报统计")
    @RequestMapping(value = "stat", method = RequestMethod.GET)
    @ResponseBody
    public Collection<Map<String, Object>> stat(Integer projectId, String time) throws Exception {
        if (StringUtils.isEmpty(time)) {
            time = DateUtil.date2Str(new Date(), "yyyyMMdd");
        }
        time = time.replaceAll("-", "");
        int days = DateUtil.getWorkDays(time, Arrays.asList(settingService.getById(Setting.class, "holidays").getValue().split(",")));
        Map<String, Map<String, Object>> mapMap = new HashMap();
        try {
            List<UserProject> ulist = userService.findUserProjectByProjectId(projectId);
            if (ulist != null) {
                for (UserProject ug : ulist) {
                    User user = ug.getUser();
                    Map<String, Object> map = new HashMap();
                    map.put("id", user.getId());
                    map.put("name", user.getName());
                    map.put("taskall", 0);
                    map.put("taskdone", 0);
                    map.put("days", days);
                    map.put("daily", 0);
                    map.put("weekly", 0);
                    mapMap.put(String.valueOf(user.getId()), map);
                }
            }
            List<Object[]> tlist = taskService.statList(projectId);
            if (tlist != null) {
                for (Object[] ds : tlist) {
                    Map<String, Object> map = mapMap.get(String.valueOf(ds[0]));
                    map.put("taskall", LangUtils.toInt(ds[1], 0));
                    map.put("taskdone", LangUtils.toInt(ds[2], 0));
                }
            }

            int month = LangUtils.toInt(time.substring(0,6),0);
            tlist = reportService.statListDaily(projectId, month);
            if (tlist != null) {
                for (Object[] ds : tlist) {
                    Map<String, Object> map = mapMap.get(String.valueOf(ds[0]));
                    map.put("daily", LangUtils.toInt(ds[1], 0));
                }
            }

            tlist = reportService.statListWeekly(projectId,month);
            if (tlist != null) {
                for (Object[] ds : tlist) {
                    Map<String, Object> map = mapMap.get(String.valueOf(ds[0]));
                    map.put("weekly", LangUtils.toInt(ds[1], 0));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapMap.values();
    }
}
